package com.talkweb.ncfw.intercept;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.talkweb.ncfw.data.ProcessResult;
import com.talkweb.ncfw.exceptions.BaseException;
import com.talkweb.ncfw.exceptions.ViewBaseException;
import com.talkweb.ncfw.utils.reflection.ReflectionUtils;
import com.talkweb.ncfw.web.utils.HttpServletUtils;

/**
 * <p>文件名称: ExceptionInterceptor.java</p>
 * <p>文件描述: 异常过滤器</p>
 * <p>版权所有: 版权所有(C)2010</p>
 * <p>内容摘要: 简要描述本文件的内容，包括主要模块、函数及能的说明</p>
 * <p>其他说明: 其它内容的说明</p>
 * <p>完成日期: 2012-1-12</p>
 * <p>修改记录1:</p>
 * <pre>
 *    修改日期:
 *    修 改 人:
 *    修改内容:
 * </pre>
 * <p>修改记录2：…</p>
 * @author  Wuqingming
 */
public class ExceptionInterceptor extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1626314425255056664L;
	
	private static final Log log = LogFactory.getLog(ExceptionInterceptor.class);
	/** 过滤的异常 */
	private static Exception[] filterExceptions = null;

	public void init() {
		log.debug("init ExceptionFilter...");
		String _filterExceptions = "java.lang.Exception";
		if (_filterExceptions == null || _filterExceptions.length() == 0) {
			return;
		}
		String[] filterExceptionArray = _filterExceptions.split(",");
		filterExceptions = new Exception[filterExceptionArray.length];
		for (int i = 0; i < filterExceptionArray.length; i++) {
			try {
				filterExceptions[i] = (Exception)ReflectionUtils.classForName(filterExceptionArray[i]).newInstance();
			} catch (InstantiationException e) {
				log.error("初始化异常过滤器时发生异常.", e);
			} catch (IllegalAccessException e) {
				log.error("初始化异常过滤器时发生异常.", e);
			} catch (ClassNotFoundException e) {
				log.error("初始化异常过滤器时发生异常.", e);
			}
		}
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		String result = null;
		try {
			result = invocation.invoke();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			if (!checkIfNeedToFilter(e)) {
				throw e;
			}
			String exceptMsg = null;
			if (e instanceof ViewBaseException) {//视图层异常
				ViewBaseException _e = (ViewBaseException)e;
				log.info("视图层异常, 异常信息：" + _e.getMsg());
				exceptMsg = _e.getMsg();
			} else if (e instanceof BaseException) {
				BaseException _e = (BaseException)e;
				log.info("系统基础异常, 异常信息：" + _e.getMsg());
				exceptMsg = _e.getMsg();
			} else {
				log.info("其它异常, 异常信息：" + e.getMessage());
				exceptMsg = "系统内部错误，请与系统管理员联系.";
			}
			log.warn(exceptMsg);
			ProcessResult processResult = new ProcessResult(false, exceptMsg);
			HttpServletUtils.outJson(ServletActionContext.getResponse(), processResult);
		}
		return result;
	}
	
	/**
	 * 
	 * @author：Wuqingming 	        
	 * @date：2011-11-3
	 * @Description：检测判断是否对给定的异常进行过滤处理
	 * @param e 异常
	 * @return
	 */
	protected boolean checkIfNeedToFilter (Exception e) {
		if (filterExceptions == null || filterExceptions.length == 0) {
			return true;		//默认过滤所有异常
		}
		for (Exception filterException : filterExceptions) {
			if ((filterException.getClass().isAssignableFrom(e.getClass()))) {
				return true;
			}
		}
		return false;
	}


	
}

